<template>
  <el-dialog
    :title="addOrEdit === 0 ? '新增用户' : '编辑用户'"
    :visible.sync="dialogFormVisible"
    center
    class="my-dialog"
    width="477px"
    top="10vh"
    @closed="closedHandler"
  >
    <el-form :model="form" :rules="rules" ref="userForm">
      <el-form-item
        label="用户名"
        prop="username"
        :label-width="formLabelWidth"
      >
        <el-input v-model="form.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email" :label-width="formLabelWidth">
        <el-input v-model="form.email" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone" :label-width="formLabelWidth">
        <el-input v-model="form.phone" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item label="角色" prop="role_id" :label-width="formLabelWidth">
        <el-select v-model="form.role_id" placeholder="请选择角色">
          <el-option label="管理员" :value="2"></el-option>
          <el-option label="老师" :value="3"></el-option>
          <el-option label="学生" :value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status" :label-width="formLabelWidth">
        <el-select v-model="form.status" placeholder="请选择状态">
          <el-option label="启用" :value="1"> </el-option>
          <el-option label="禁用" :value="0"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="用户备注"
        prop="remark"
        :label-width="formLabelWidth"
      >
        <el-input v-model="form.remark" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="removeForm">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
// 导入发起请求
import { addUser, editUser } from '../../../api/user'
export default {
  name: 'userDialog',
  data () {
    return {
      addOrEdit: 0,
      title: '新增',
      form: {
        username: '',
        email: '',
        phone: '',
        role_id: '',
        status: '',
        remark: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          {
            type: 'email',
            message: '邮箱格式有误',
            trigger: 'blur'
          }
        ],
        phone: [
          { required: true, message: '手机不能为空', trigger: 'blur' },
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '手机号格式有误',
            trigger: 'blur'
          }
        ],
        role_id: [{ required: true, message: '角色不能为空', trigger: 'blur' }]
      },
      dialogFormVisible: false,
      formLabelWidth: '85px'
    }
  },
  // filters: {
  //   statusStyle (val) {
  //     if (val === 0) {
  //       return '禁用'
  //     } else {
  //       return '启用'
  //     }
  //   }
  // },
  methods: {
    closedHandler () {
      this.$refs.userForm.resetFields()
    },
    submitForm () {
      // 新增的清空
      if (this.addOrEdit === 0) {
        this.$refs.userForm.validate(valid => {
          if (valid) {
            addUser(this.form).then(res => {
              console.log(res)
              // 关闭弹框
              this.dialogFormVisible = false
              // 传达给父组件中重新渲染页面
              this.$emit('successComDialog')
              // 提示
              this.$message.success('新增成功')
            })
          } else {
            this.$message.warning('格式有误')
            return false
          }
        })
      } else {
        // 否则就是编辑按钮事件
        this.$refs.userForm.validate(valid => {
          if (valid) {
            editUser(this.form).then(res => {
              console.log(res)
              // 关闭弹框
              this.dialogFormVisible = false
              // 传达给父组件中重新渲染页面
              this.$emit('successComDialog')
              // 提示
              this.$message.success('编辑成功')
            })
          } else {
            this.$message.warning('格式有误')
            return false
          }
        })
      }
    },
    removeForm () {
      this.dialogFormVisible = false
    }
  }
}
</script>

<style></style>
